/**
 * 
 */
package hihocoder;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zhong
 *
 */
public class SubdomainVisitCount {
	public List<String> subdomainVisits(String[] cpdomains) {
		Map<String, Integer> dom2cnt = new HashMap<>();
		for (String string : cpdomains) {
			String[] strings = string.split(" ");
			int cnt = Integer.parseInt(strings[0]);
			int start = 0;
			do {
				int idx = strings[1].indexOf('.', start);// .的位置
				String subd = strings[1].substring(start);
				dom2cnt.put(subd, dom2cnt.getOrDefault(subd, 0) + cnt);
				start = idx + 1;
			} while (start != 0);
		}
		List<String> reStrings = new ArrayList<>();
		for (String string : dom2cnt.keySet()) {
			reStrings.add(dom2cnt.get(string) + " " + string);
		}
		return reStrings;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String[] cpdomains = { "900 google.mail.com", "50 yahoo.com", "1 intel.mail.com", "5 wiki.org" };
		System.out.println(new SubdomainVisitCount().subdomainVisits(cpdomains));
	}

}
